package com.mikiya.plugin.util;

import com.mikiya.plugin.dto.ClassInfoDTO;
import com.mikiya.plugin.dto.ClassMethodDTO;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class SortUtil {

    public static List<ClassInfoDTO> sortByCyclomaticComplexity(List<ClassInfoDTO> classList) {
        if (ObjectUtil.isEmpty(classList)) {
            return classList;
        }
        classList.stream().forEach(classInfoDTO -> {
            List<ClassMethodDTO> methodList = classInfoDTO.getMethodList();
            if (ObjectUtil.isEmpty(methodList)) {
                return;
            }
            int cyclomaticComplexity = 0;
            for (ClassMethodDTO method : methodList) {
                if (method.getCyclomaticComplexity() > 6) {
                    cyclomaticComplexity += method.getCyclomaticComplexity();
                }
            }
            classInfoDTO.setCyclomaticComplexity(cyclomaticComplexity);
            classInfoDTO.setMethodList(methodList.stream().sorted(Comparator.comparing(ClassMethodDTO::getCyclomaticComplexity).reversed()).collect(Collectors.toList()));
        });
        return classList.stream().sorted(Comparator.comparing(ClassInfoDTO::getCyclomaticComplexity).reversed()).collect(Collectors.toList());
    }
}
